
#include <ukapi.h>

typedef struct
{
	signed long		* long_cmd_val;
	double	* double_cmd_val;
	
	signed long		* long_cpt_val;
	double	* double_cpt_val;

	signed long		(*LongTrfFunction)( signed long cpt );
	double	(*DoubleTrfFunction)( double cpt );

	char			Run;
} regulator_t, * pregulator_t;

int regulator_init( pregulator_t reg )
{
	if ( reg == null )
	{
		return -1;
	}

	reg->double_cmd_val = null;
	reg->long_cmd_val	= null;
	reg->double_cpt_val = null;
	reg->long_cpt_val	= null;
	
	reg->Run = 0;

	return 0;
}

int regulator_destroy( pregulator_t reg )
{
	return 0;
}

int regulator_task( pregulator_t reg )
{
	register char UseLong	= (		( reg->long_cmd_val != null ) 
								&&	( reg->long_cpt_val != null ) 
								&&	( reg->LongTrfFunction != null ) 
								) ? 1 : 0;

	register char UseDouble	= (		( reg->double_cmd_val != null ) 
								&&	( reg->double_cpt_val != null ) 
								&&	( reg->DoubleTrfFunction != null ) 
								) ? 1 : 0;

	if ( UseLong && UseDouble )
	{
		return -1;
	}

	while ( reg->Run )
	{
		if ( UseLong )
		{
			*(reg->long_cmd_val) = reg->LongTrfFunction( *(reg->long_cpt_val) );
		}
		else
		{
			*(reg->double_cmd_val) = reg->LongTrfFunction( *(reg->long_cpt_val) );	
		}
	}

	return 0;
}

int regulator_start( pregulator_t reg )
{
	reg->Run = 1;

	return 0;
}

int regulator_stop( pregulator_t reg )
{
	reg->Run = 0;

	return 0;
}

int regulator_set_cmd_long_addr( pregulator_t reg, signed long * cmd_val )
{
	reg->long_cmd_val = cmd_val;

	return 0;
}

int regulator_set_cmd_double_addr( pregulator_t reg, double * cmd_val )
{
	reg->double_cmd_val = cmd_val;

	return 0;
}

int regulator_set_cpt_long_addr( pregulator_t reg, signed long * cpt_val )
{
	reg->long_cpt_val = cpt_val;

	return 0;
}

int regulator_set_cpt_double_addr( pregulator_t reg, double * cpt_val )
{
	reg->double_cpt_val = cpt_val;

	return 0;
}

int regulator_set_long_transfert_function( pregulator_t reg, signed long (*LongTrfFunction)( signed long cpt ) )
{
	reg->LongTrfFunction = LongTrfFunction;

	return 0;
}

int regulator_set_double_transfert_function( pregulator_t reg, double (*DoubleTrfFunction)( double cpt ) )
{
	reg->DoubleTrfFunction = DoubleTrfFunction;

	return 0;
}

int regulator_cmd_access_lock();
int regulator_cmd_access_unlock();

int regulator_cpt_access_lock();
int regulator_cpt_access_unlock();
